import { request } from "@/utils/request";
import { getFillDept } from "@/system/api/organization/organization";
import {
    pageShow,
    pageRemove,
    editAut,
    pageExport,
    pageImport,
    getWorkRep,
    pageCustom,
  } from "@/system/api/customPage";
  import { userevent } from '@/system/config/userevent.js';
const pageUI = {
    //界面方法
    add: function (param, self,  item) {
        // 有配置表单
        if (item.method) {
            // self.buttonComWinShow = true;
            // self.relatedPageRoute = JSON.parse(item.method).dblvue;
            // self.relatedWinParams = {
            //     deptId: JSON.parse(item.method).dblcomId,
            //     funcId: JSON.parse(item.method).multipleChoice,
            //     funcCode: JSON.parse(item.method).menuCode,
            // };
            let info=JSON.parse(item.method);
            if(info.type==1){
                let dblParamIn=JSON.parse(info.dblParamIn);
                 self.templateWindowShow=true;
                console.log(dblParamIn.id,dblParamIn.menuCode,dblParamIn.multipleChoice,info.dblcomText)
                self.templateWindowParams.id=dblParamIn.id;
                self.templateWindowParams.menuCode=dblParamIn.menuCode;
                self.templateWindowParams.multipleChoice=dblParamIn.multipleChoice;
                self.templateWindowParams.dialogTitle=info.dblcomText;
            };
            if(info.type==2){
                //表单
                console.log("workId:::"+ JSON.parse(item.method).workId)
                self.formBtnType="add";
                self.cellDbClick(JSON.parse(JSON.parse(item.method).dblParamIn));
            }
            return;
        }
        if (self.params && self.params.deptId) {
            // 有默认配置部门 ID
            self.tdeptId = self.params.deptId;
            return;
        }
        let data={};
        if(JSON.stringify(param)=="{}"){
            data={
                funcId: self.params && self.params.id ? null : self.menuId,
                funcCode: self.params && self.params.id ? self.params.menuCode : null,
                type: 2,
            }
        }else{
            data={
                funcId: param.funcId,
                funcCode: param.funcCode,
                type: param.type,
            }
        }
        getFillDept(
            {
                funcId: self.params && self.params.id ? null : self.menuId,
                funcCode: self.params && self.params.id ? self.params.menuCode : null,
                type: 2,
            }
        ).then((res) => {
            if (res.info.length === 1) {
                if (item.method && item.method != null) {
                    // 关联窗体
                    self.relatedPageRoute = JSON.parse(item.method).route;
                    // self.relatedPageRoute = JSON.parse(item.method).dblvue;
                    self.relatedWinParams = {
                        deptId: res.info[0].id,
                        funcId: self.params && self.params.id ? null : self.menuId,
                        funcCode:
                            self.params && self.params.id ? self.params.menuCode : null,
                    };
                    if (item.type === 1) {
                        // 模块
                        self.buttonComShow = true;
                    } else {
                        // 弹窗
                        self.buttonComWinShow = true;
                    }
                } else {
                    self.funcId = self.params && self.params.id ? null : self.menuId;
                    self.funcCode =
                        self.params && self.params.id ? self.params.menuCode : null;
                    self.funcName = self.pageContent.text;
                    self.tdeptId = res.info[0].id;
                    if (self.params && self.params.businessInfo) {
                        if (Object.keys(self.params.businessInfo).length > 0) {
                            self.businessInfoParam = self.params.businessInfo;
                        }
                    }
                    if (self.params && self.params.cellParams) {
                        self.formateInitParam();
                    } else {
                        self.cellViewShow = true;
                    }
                }
            } else {
                // 多个部门
                self.deptMap = res.info;
                self.deptMapShow = true;
            }
        });
    },
    edit: async function (param, self, item) {
        if (self.checkedData.length === 0) {
            self.message("请选择需要编辑的数据", 3);
            return;
        }
        if (self.checkedData.length > 1) {
            self.message("不能同时编辑多条数据", 3);
            return;
        }
        const detail = JSON.parse(JSON.stringify(self.checkedData[0]));
        self.wpId = await self.getWpId(detail);
        if (!self.wpId) {
            self.message("没有表单 ID 无法编辑", 3);
            return;
        }
        if (!detail[self.pageContent.primaryName]) {
            self.message("没有主键无法编辑", 3);
            return;
        }
        let data={};
        if(JSON.stringify(param)=="{}"){
            data={
                funcId: self.params && self.params.id ? null : self.menuId,
                funcCode: self.params && self.params.id ? self.params.menuCode : null,
                tableName: self.pageContent.tableName,
                id: detail[self.pageContent.primaryName],
                keyName: self.pageContent.primaryName,
            }
        }else{
            data={
                funcId: param.funcId,
                funcCode: param.funcCode,
                tableName: param.tableName,
                id: detail[self.pageContent.primaryName],
                keyName: param.keyName,
            }
        }
        editAut({
            funcId: self.params && self.params.id ? null : self.menuId,
            funcCode: self.params && self.params.id ? self.params.menuCode : null,
            tableName: self.pageContent.tableName,
            id: detail[self.pageContent.primaryName],
            keyName: self.pageContent.primaryName,
        }).then((res) => {
            if (res.success) {
                self.workId = detail[self.pageContent.primaryName];
                self.funcName = self.pageContent.text;
                self.tdeptId = self.$store.state.user.info.deptId;
                if (self.params && self.params.businessInfo) {
                    if (Object.keys(self.params.businessInfo).length > 0) {
                        self.businessInfoParam = self.params.businessInfo;
                    }
                }
                if (self.params && self.params.cellParams) {
                    self.formateInitParam();
                } else {
                    self.cellViewShow = true;
                }
            } else {
                self.message("没有编辑权限", 3);
                return;
            }
        });
    },
    delete: function (param, self, item) {
        if (self.checkedData.length === 0) {
            self.message("请选择需要删除的数据", 3);
            return;
        }
        let ids = [];
        self.checkedData.forEach((item) => {
            ids.push(item[self.pageContent.primaryName]);
        });
        self.messageBox("确认删除？").then((c) => {
            if (c.action) {
                pageRemove({
                    pageId: self.pageContent.id,
                    values: ids,
                    menuId: self.params && self.params.id ? null : self.menuId,
                    menuCode:
                        self.params && self.params.id ? self.params.menuCode : null,
                }).then((res) => {
                    if (res.success) {
                        self.search(self.pageIndex);
                        self.message(res.msg, 1);
                    } else {
                        self.message("数据已经存在流程中，不允许删除", 3);
                    }
                });
            }
        });
    },
    viewFlow: function (param, self, item) {
        //查看流程
        if (self.checkedData.length > 1) {
            self.message("不能同时查看多个流程", 3);
            return;
        }
        if (self.checkedData.length === 0) {
            self.message("请选择需要查看的流程", 3);
            return;
        }
        if (self.checkedData[0].WF_STATE === 0) {
            self.message("流程未上报", 3);
            return;
        }
        self.flowViewShow = true;
        self.wfInstId = self.checkedData[0].WFINST_ID;
    },
    export: async function (param, self, item) {
        //导出
        const arr = await self.getFormattingCondition();
        pageExport({
            pageId: self.pageContent.id,
            menuId: self.params && self.params.id ? null : self.menuId,
            menuCode: self.params && self.params.id ? self.params.menuCode : null,
            params: arr,
        }).then((res) => {
            if (res.success) {
                window.open(process.env.VUE_APP_BASE_API + res.info);
                self.message(res.msg, 1);
            } else {
                self.message(res.msg, 3);
                return;
            }
        });
    },
    import: function (param, self, item) {
        //导入
        getFillDept({
            funcId: self.params && self.params.id ? null : self.menuId,
            funcCode: self.params && self.params.id ? self.params.menuCode : null,
            type: 2,
        }).then((res) => {
            if (res.info.length === 1) {
                self.tdeptId = res.info[0].id;
                self.form = {
                    handleWay: 1,
                    file: "",
                };
                self.file = null;
                self.errorMessageShow = false;
                self.importFileDialog = true;
            } else {
                self.deptMap = res.info;
                self.deptMapShow = true;
            }
        });
    },
    custom: function (param, self, item) {
        if (!item.url) {
            self.message("没有配置请求地址", 3);
            return;
        }
        if (!self.checkedData.length) {
            self.message("最少选择一条数据", 3);
            return;
        }
        const paramName = JSON.parse(item.params);
        let keyArrays = Object.keys(paramName);
        const keyName = keyArrays[0];
        let paramData = self.checkedData;
        if (item.contentType == "2") {
            //JSON请求
            request({
                url: item.url,
                method: "post",
                type: 2,
                data: paramData,
            }).then((res) => {
                if (res.success) {
                    //成功
                    self.message(res.msg, 1);
                    //执行页面数据请求
                } else {
                    //失败
                    self.message(res.msg, 3);
                }
            });
        }
    }
};
const form = {
    add: function () {
        console.log("我是表单add")
    }
};
export {
    pageUI,
    form
}